package spzx.controller;

import com.atguigu.spzx.model.dto.system.AssginRoleDto;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spzx.service.SysRoleService;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("admin/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 给用户分配角色
     * @params: {userId:xxx , roleIdList:[]}
     * @return
     */
    @PostMapping("doAssignRole")
    public Result doAssignRole(@RequestBody AssginRoleDto assginRoleDto){
        sysRoleService.doAssignRole(assginRoleDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }





    /**
     * 查询所有的角色列表 + 当前已分配的角色ID列表
     * @param sysUserId
     * @return 两个集合组成的map
     */
    @GetMapping("findRoleListAndRoleIdList/{sysUserId}")
    public Result findRoleListAndRoleIdList(@PathVariable Long sysUserId){
        List<Long> roleIdList = sysRoleService.findRoleIdListBySysUserId(sysUserId);
        List<SysRole> roleList = sysRoleService.findAll();
        HashMap map = new HashMap<>();
        map.put("roleIdList",roleIdList);
        map.put("roleList",roleList);
        return Result.build(map, ResultCodeEnum.SUCCESS);
    }


    /**
     * 查询当前系统用户已分配的角色ID列表
     * @param sysUserId
     * @return
     */
    @GetMapping("findRoleIdListBySysUserId/{sysUserId}")
    public Result findRoleIdListBySysUserId(@PathVariable Long sysUserId){
        List<Long> roleIdList = sysRoleService.findRoleIdListBySysUserId(sysUserId);
        return Result.build(roleIdList, ResultCodeEnum.SUCCESS);
    }





    /**
     * 前端的弹窗回显sys_role表中的数据,作为复选框的选项文字
     * @return
     */
    @GetMapping("findAll")
    public Result findAll(){
        List<SysRole> list = sysRoleService.findAll();
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }


    /**
     * 修改角色信息
     * @param sysRole
     * @return
     */
    @PutMapping("updateSysRole")
    public Result updateSysRole(@RequestBody SysRole sysRole){
        sysRoleService.updateSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


    /**
     * 根据角色ID删除角色信息(多个删除)
     * @param roleIds
     * @return
     */
    @DeleteMapping("deleteByIds")
    public Result deleteByIds(@RequestBody List<Long> roleIds){
        sysRoleService.deleteByIds(roleIds);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


    /**
     * 根据角色ID删除角色信息(单个删除)
     * @param roleId
     * @return
     */
    @DeleteMapping("deleteById/{roleId}")
    public Result deleteById(@PathVariable Long roleId){
        sysRoleService.deleteById(roleId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


    /**
     * 新增角色
     * @param sysRole
     * @return
     */
    @PostMapping("addSysRole")
    public Result addSysRole(@RequestBody SysRole sysRole){
        sysRoleService.addSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }



    /**
     * 根据角色名称查询角色信息,查询角色列表
     * @param pageNum
     * @param pageSize
     * @param sysRoleDto
     * @return
     */
    @PostMapping("findByPage/{pageNum}/{pageSize}")
    public Result findByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize,@RequestBody SysRoleDto sysRoleDto){
        PageInfo pageInfo = sysRoleService.findByPage(pageNum, pageSize, sysRoleDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }
}
